<?php
/**
 * Hole img upload action
 */
$golf_course_id = get_input('golf_course_id');
$hole = get_input('hole');

$error = 0;
if ($_FILES['hole_img']['error'] != 0 || $_FILES['hole_img_large']['error'] != 0) {
	$error = -1;
} else {
	$milliseconds = round(microtime(true) * 1000);
	$name = $_FILES["hole_img"]["name"];
	if(strrpos($name,'.') > -1) {
		$name = $milliseconds . substr($name, strrpos($name,'.'));
		$name_large = $milliseconds . '_large' . substr($name, strrpos($name,'.'));
	} else {
		$name = $milliseconds . '.jpg';
		$name_large = $milliseconds . '_large' . '.jpg';
	}
	move_uploaded_file($_FILES["hole_img"]["tmp_name"],
			elgg_get_root_path() . "_graphics/hole/" . $name);
	move_uploaded_file($_FILES["hole_img_large"]["tmp_name"],
			elgg_get_root_path() . "_graphics/hole/" . $name_large);
	
	$golf_course_card = elgg_get_golf_course_card(array('golf_course_id' => $golf_course_id, 'hole' => $hole));
	if($golf_course_card) {
		$golf_course_card = $golf_course_card[0];
		$golf_course_card_id = $golf_course_card->golf_course_card_id;
	}
	$golf_course_card = new ElggGolfCourseCardItem();
	if($golf_course_card_id) {
		$golf_course_card->golf_course_card_id = $golf_course_card_id;
	}
	$golf_course_card->golf_course_id = $golf_course_id;
	$golf_course_card->hole = $hole;
	$golf_course_card->map = $name;
	save_golf_course_card($golf_course_card);
	
	$error = array('error' => $error);
	$error = json_encode($error);
	print $error;
}
?>